package com.cskaoyan.demo10.servlet;

import com.cskaoyan.demo10.bean.vo.BaseRespVo;
import com.cskaoyan.demo10.bean.vo.GoodsCreateVo;
import com.cskaoyan.demo10.common.CommonServlet;
import com.cskaoyan.demo10.service.GoodsService;
import com.cskaoyan.demo10.service.GoodsServiceImpl;
import com.cskaoyan.demo10.util.JacksonUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/goods/*")
public class AdminGoodsServlet extends CommonServlet {

    GoodsService goodsService = new GoodsServiceImpl();

    public void list(HttpServletRequest request, HttpServletResponse response) {
    }

    public void create(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 接收的请求参数中文乱码问题解决
        request.setCharacterEncoding("utf-8");
        // 参数接收
        String jsonStr = request.getReader().readLine();
        // JsonStr转换成对应的对象
        GoodsCreateVo goodsCreateVo = JacksonUtil.read(jsonStr, GoodsCreateVo.class);
        // 业务处理 → service
        goodsService.createGoods(goodsCreateVo);

        // 结果响应
        BaseRespVo baseRespVo = BaseRespVo.ok(null);
        response.getWriter().println(JacksonUtil.write(baseRespVo));

    }
}
